import { IBackgroundList } from '../interface';

class BackGroundListDao {
	private backGroundList: IBackgroundList[] = [];
	private defualtBackGround: IBackgroundList[] = [
		{
			id: -2,
			api: '',
			apiClear: '',
			fileName: 'nope.png', // 文件名
			url: '', // 应用图片地址
			abbreviatedUrl: '', // 预览图片地址
			type: 'no', // 不开启虚拟背景
			isDefault: true
		},
		{
			id: -1,
			api: '',
			apiClear: '',
			fileName: 'blur.png',
			url: '', // 应用图片地址
			abbreviatedUrl: '', // 预览图片地址
			type: 'fuzzy', // 模糊
			blurRadius: 12, // 模糊值
			isDefault: true
		}
	];
	private applyBackgroundId: number = -2;

	public findAllBackgroundList(): IBackgroundList[] {
		return this.backGroundList;
	}

	public addBackground(background: IBackgroundList) {
		const { id } = background;
		if (!this.isExists(id)) {
			this.backGroundList = [...this.backGroundList, background];
			return true;
		}
		return false;
	}

	public setBackgroundList(backGroundList: IBackgroundList[]) {
		const list = [...this.defualtBackGround, ...backGroundList];
		this.backGroundList = list;
	}

	public isExists(id: number): boolean {
		const background = this.backGroundList.find(item => item.id === id);
		return background ? true : false;
	}

	public removeBackground(id: number): boolean {
		if (this.isExists(id)) {
			this.backGroundList = this.backGroundList.filter(item => item.id !== id);
			return true;
		}
		return false;
	}

	public setBackgroundId(id: number) {
		this.applyBackgroundId = id;
	}

	public getBackgroundId() {
		return this.applyBackgroundId;
	}

	public findBackgroundById(id: number) {
		if (this.isExists(id)) {
			return this.backGroundList.find(item => item.id === id);
		}
		return null;
	}
}

export default BackGroundListDao;
